package com.dongqi.myblog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TTag;
import com.dongqi.myblog.service.TTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@RequestMapping("/admin")
@Controller
public class TagController {

    @Autowired
    private TTagService tagService;

    @GetMapping("/tag")
    public String pageTags(@RequestParam(defaultValue = "1")Long pageNo,
                           @RequestParam(defaultValue = "8")Long pageSize,
                           Model model){
        Page<TTag> page = tagService.pageTags(new Page<>(pageNo,pageSize));
        model.addAttribute("page",page);
        model.addAttribute("hasPrevious",page.hasPrevious());
        model.addAttribute("hasNext",page.hasNext());
        return "admin/tags";
    }

    @GetMapping("/tag/toInput")
    public String toInput(){
        return "admin/tags-input";
    }

    @PostMapping("/tag")
    public String insert(TTag tag, RedirectAttributes attributes){
        if (tag.getId()==null){
            QueryWrapper<TTag> wrapper = new QueryWrapper();
            wrapper.eq("name",tag.getName().trim());
            TTag demo = tagService.getOne(wrapper);
            if (demo!=null){
                attributes.addFlashAttribute("message","该标签已存在");
                return "redirect:/admin/tag/toInput";
            }
            Boolean result = tagService.saveTag(tag);
            if (result){
                attributes.addFlashAttribute("message","保存成功");
            }else {
                attributes.addFlashAttribute("message","保存失败");
            }
        }
        return "redirect:/admin/tag";
    }

    /**
     * 删除一个tag
     */
    @GetMapping("/tag/{id}/delete")
    public String delete(@PathVariable Long id){
        tagService.deleteById(id);
        return "redirect:/admin/tag";
    }

    @GetMapping("tag/{id}/input")
    public String toUpdate(@PathVariable Long id,
                           Model model){
        TTag tag = tagService.getById(id);
        model.addAttribute("tag",tag);
        return "admin/tags-input";
    }

    /**
     * 修改一个tag
     */
    @PostMapping("/tag/{id}")
    public String update(@PathVariable Long id,
                         TTag tag,
                         RedirectAttributes attributes){

        QueryWrapper<TTag> wrapper = new QueryWrapper();
        wrapper.eq("name",tag.getName().trim());
        TTag demo = tagService.getOne(wrapper);
        if (demo!=null){
            attributes.addFlashAttribute("message","该标签已存在");
            return "redirect:/admin/tag/toInput";
        }
        Boolean result = tagService.updateTag(tag);
        if (result){
            attributes.addFlashAttribute("message","保存成功");
        }else {
            attributes.addFlashAttribute("message","保存失败");
        }
        return "redirect:/admin/tag";

    }








}
